package com.ruoyi.develop.goodsSalebuyall.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.goodsSalebuy.domain.GoodsSalebuy;
import com.ruoyi.develop.goodsSalebuy.mapper.GoodsSalebuyMapper;
import com.ruoyi.develop.goodsSaledo.domain.GoodsSaledo;
import com.ruoyi.develop.goodsSaledo.mapper.GoodsSaledoMapper;
import com.ruoyi.develop.lvBuy.domain.LvBuy;
import com.ruoyi.develop.lvBuy.mapper.LvBuyMapper;
import com.ruoyi.develop.lvBuy.service.ILvBuyService;
import com.ruoyi.develop.lvDaohuo.mapper.LvDaohuoMapper;
import com.ruoyi.develop.lvRukumingxi.domain.LvRukumingxi;
import com.ruoyi.develop.lvRukumingxi.service.ILvRukumingxiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.goodsSalebuyall.mapper.GoodsSalebuyallMapper;
import com.ruoyi.develop.goodsSalebuyall.domain.GoodsSalebuyall;
import com.ruoyi.develop.goodsSalebuyall.service.IGoodsSalebuyallService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;

/**
 * 采购单合计Service业务层处理
 *
 * @author qh
 * @date 2024-08-09
 */
@Service
public class GoodsSalebuyallServiceImpl implements IGoodsSalebuyallService {
    @Resource
    private GoodsSalebuyallMapper goodsSalebuyallMapper;
    @Resource
    private GoodsSalebuyMapper goodsSalebuyMapper;

    @Resource
    private GoodsSaledoMapper goodsSaledoMapper;
    @Resource
    private LvBuyMapper lvBuyMapper;
    @Resource
    private LvDaohuoMapper lvDaohuoMapper;
    @Autowired
    private IGoodsSaleService iGoodsSaleService;
    @Autowired
    private ILvBuyService lvBuyService;
    @Autowired
    private ILvRukumingxiService lvRukumingxiService;



    /**
     * 查询采购单合计
     *
     * @param id 采购单合计主键
     * @return 采购单合计
     */
    @Override
    public GoodsSalebuyall selectById(String id) {
        return goodsSalebuyallMapper.selectById(id);
    }

    /**
     * 查询采购单合计列表
     *
     * @param entity 采购单合计
     * @return 采购单合计
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<GoodsSalebuyall> selectList(GoodsSalebuyall entity) {
        return goodsSalebuyallMapper.selectList(entity);
    }

    @Override
    public List<GoodsSalebuyall> selectMainId(String mainId) {
        return goodsSalebuyallMapper.selectMainId(mainId);
    }

    /**
     * 新增采购单合计
     *
     * @param entity 采购单合计
     * @return 结果
     */
    @Override
    public int insert(GoodsSalebuyall entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return goodsSalebuyallMapper.insert(entity);
    }

    /**
     * 修改采购单合计
     *
     * @param entity 采购单合计
     * @return 结果
     */
    @Override
    public int update(GoodsSalebuyall entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return goodsSalebuyallMapper.update(entity);
    }

    @Override
    public int editZj(GoodsSalebuyall entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        List<GoodsSalebuy> goodsSalebuyList = goodsSalebuyMapper.selectByCgallId(entity.getId());
        for (GoodsSalebuy goodsSalebuy : goodsSalebuyList) {
            goodsSalebuy.setZhijiandan(entity.getZhijiandan());
            goodsSalebuyMapper.update(goodsSalebuy);
        }
        return goodsSalebuyallMapper.update(entity);
    }

    /**
     * 批量删除采购单合计
     *
     * @param ids 需要删除的采购单合计主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return goodsSalebuyallMapper.deleteByIds(ids);
    }

    /**
     * 删除采购单合计信息
     *
     * @param id 采购单合计主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return goodsSalebuyallMapper.deleteById(id);
    }


    @Override
    public int deleteCg(String id) {
        List<GoodsSalebuy> goodsSalebuyList = goodsSalebuyMapper.selectByCgallId(id);
        for (GoodsSalebuy goodsSalebuy : goodsSalebuyList) {
            if(StringUtils.isNotBlank(goodsSalebuy.getQinggouId())){
                GoodsSaledo goodsSaledo = goodsSaledoMapper.selectById(goodsSalebuy.getQinggouId());
                goodsSaledo.setStatus("13");
                goodsSaledo.setBuyRemain(goodsSaledo.getBuyRemain().add(goodsSalebuy.getShengyu()));
                goodsSaledoMapper.update(goodsSaledo);
            }
            goodsSalebuyMapper.deleteById(goodsSalebuy.getId());
        }
        return goodsSalebuyallMapper.deleteById(id);
    }


    @Override
    public int deleteBuyAllReturn(String id) {
        List<GoodsSalebuy> goodsSalebuyList = goodsSalebuyMapper.selectByCgallId(id);
        for (GoodsSalebuy goodsSalebuy : goodsSalebuyList) {
            if(StringUtils.isNotBlank(goodsSalebuy.getRukumingxiId())){
                LvRukumingxi lvRukumingxi = lvRukumingxiService.selectById(goodsSalebuy.getRukumingxiId());
                lvRukumingxi.setCanCount(lvRukumingxi.getCanCount().add(goodsSalebuy.getShengyu()));
                lvRukumingxi.setReturnCount(lvRukumingxi.getReturnCount().subtract(goodsSalebuy.getShengyu()));
                lvRukumingxi.setReturnWeight(lvRukumingxi.getReturnWeight().subtract(goodsSalebuy.getDun()));
                lvRukumingxiService.update(lvRukumingxi);
            }
            goodsSalebuyMapper.deleteById(goodsSalebuy.getId());
        }
        return goodsSalebuyallMapper.deleteById(id);
    }



    @Override
    @Transactional
    public void submitCgd(GoodsSalebuyall goodsSaledo) {
        List<GoodsSalebuyall> rows = goodsSaledo.getColumns();
        if (!rows.isEmpty()) {
            LvBuy lvBuy = new LvBuy();
            lvBuy.setId(IdUtils.fastSimpleUUID());
            lvBuy.setSource(BusinessConstants.BUY_SOURCE_PLEASE);
            Date ddDate = goodsSaledo.getDdDate();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String today = sdf.format(ddDate==null?new Date():ddDate);
            String codeMaxString = lvBuyMapper.selectMaxCode(today);
            if(StringUtils.isNotBlank(codeMaxString)){
                // 最大编号+1
                codeMaxString = Long.parseLong(codeMaxString)+1+"";
            }else{
                codeMaxString = today+"0001";
            }
            lvBuy.setNos(codeMaxString);
            lvBuy.setState("6");//22:请购中,4:请购提交，5采购中，6生成采购单
            lvBuy.setDdDate(ddDate==null?new Date():ddDate);//请购日期
            lvBuy.setCreateTime(new Date());
            lvBuy.setCustomerId(rows.get(0).getCustomerId());
            lvBuy.setCustomerName(rows.get(0).getCustomerName());
            lvBuy.setDeptId(rows.get(0).getDeptId());
            lvBuy.setSupplierId(goodsSaledo.getSupplierId());
            lvBuy.setSupplierName(goodsSaledo.getSupplierName());
            lvBuy.setBankName(goodsSaledo.getBankName());
            lvBuy.setBankNo(goodsSaledo.getBankNo());
            lvBuy.setYwType(goodsSaledo.getYwType());
            lvBuy.setBuyer(goodsSaledo.getBuyer());
            lvBuy.setTransactionType(BusinessConstants.BUY_TRANSACTION_TYPE_BUY);
            lvBuy.setAccountNo(goodsSaledo.getAccountNo());
            if(goodsSaledo.getDingjin()!=null && goodsSaledo.getDingjin().compareTo(BigDecimal.ZERO)>0){
                lvBuy.setDingjin(goodsSaledo.getDingjin());
                lvBuy.setIsDeposit(BusinessConstants.IS_DEPOSIT_YES);
            }else{
                lvBuy.setDingjin(BigDecimal.ZERO);
                lvBuy.setIsDeposit(BusinessConstants.IS_DEPOSIT_NO);
            }
            if(goodsSaledo.getShippingFee()!=null && goodsSaledo.getShippingFee().compareTo(BigDecimal.ZERO)>0){
                lvBuy.setShippingFee(goodsSaledo.getShippingFee());
                lvBuy.setShippingFeePaid(BigDecimal.ZERO);
                lvBuy.setShippingFeeRemain(goodsSaledo.getShippingFee());
                lvBuy.setIsShippingFee(BusinessConstants.IS_DEPOSIT_YES);
                lvBuy.setDepositMerge(goodsSaledo.getDepositMerge());
            }else{
                lvBuy.setShippingFee(BigDecimal.ZERO);
                lvBuy.setShippingFeePaid(BigDecimal.ZERO);
                lvBuy.setShippingFeeRemain(BigDecimal.ZERO);
                lvBuy.setIsShippingFee(BusinessConstants.IS_DEPOSIT_NO);
            }

            lvBuy.setRemark(goodsSaledo.getRemark());
            lvBuy.setGoodsType(goodsSaledo.getGoodsType());
            //lvBuyMapper.insert(lvBuy);
            BigDecimal money = BigDecimal.ZERO;//订单金额
            for (GoodsSalebuyall entity : rows) {
                entity.setStatus("6");//发起请购单
                entity.setMainId(lvBuy.getId());//发起请购单
                entity.setCaigouno(lvBuy.getNos());//采购单号
                entity.setShengyuDaohuoCount(entity.getShengyu());
                entity.setShengyuDaohuoDun(entity.getDun());
                entity.setApplyRemain(entity.getGoodsBuyTotal());

                entity.setSingleWeight(entity.getDun().divide(entity.getShengyu(),4, RoundingMode.HALF_UP));
                // 技术不含税单价  不含税总价
                entity.setGoodsBuyPriceNoTax(entity.getGoodsBuyPrice().divide(BigDecimal.ONE.add(entity.getTaxRate()),4, RoundingMode.HALF_UP));
                entity.setGoodsBuyTotalNoTax(entity.getGoodsBuyTotal().divide(BigDecimal.ONE.add(entity.getTaxRate()),4, RoundingMode.HALF_UP));


                update(entity);
                updateQgCg(lvBuy.getNos(), lvBuy.getId(), entity.getId(), "6");//更改请购和采购状态
                money = money.add(entity.getGoodsBuyTotal());
                // 如果采购数量  小于选定数量  把差额还回请购单
                if(entity.getShengyu().compareTo(entity.getCounts())<0){
                    BigDecimal balance = entity.getCounts().subtract(entity.getShengyu());
                    List<GoodsSalebuy> goodsSalebuys = goodsSalebuyMapper.selectByCgallId(entity.getId());
                    for (GoodsSalebuy goodsSalebuy : goodsSalebuys) {
                        if(goodsSalebuy.getShengyu().compareTo(balance)>0){
                            goodsSalebuy.setShengyu(goodsSalebuy.getShengyu().subtract(balance));
                            GoodsSaledo saledo = goodsSaledoMapper.selectById(goodsSalebuy.getQinggouId());
                            if(saledo!=null){
                                saledo.setBuyRemain(saledo.getBuyRemain().add(balance));
                            }
                            balance = BigDecimal.ZERO;
                            goodsSaledoMapper.update(saledo);
                            goodsSalebuyMapper.update(goodsSalebuy);
                        }else if(goodsSalebuy.getShengyu().compareTo(balance)<=0){
                            balance = balance.subtract(goodsSalebuy.getShengyu());
                            GoodsSaledo saledo = goodsSaledoMapper.selectById(goodsSalebuy.getQinggouId());
                            if(saledo!=null){
                                saledo.setBuyRemain(saledo.getBuyRemain().add(goodsSalebuy.getShengyu()));
                            }
                            goodsSaledoMapper.update(saledo);
                            goodsSalebuyMapper.deleteById(goodsSalebuy.getId());
                        }
                    }
                }
            }
            lvBuy.setMoney(money);
            // 如果 含运费 则 计算总金额
            if(goodsSaledo.getShippingFee()!=null && goodsSaledo.getShippingFee().compareTo(BigDecimal.ZERO)>0){
                lvBuy.setShengyukp(money.add(goodsSaledo.getShippingFee()));
                lvBuy.setShengyu(money.add(goodsSaledo.getShippingFee()));
                lvBuy.setPayApplyRemainMoney(money.add(goodsSaledo.getShippingFee()));
            }else{
                lvBuy.setShengyukp(money);
                lvBuy.setShengyu(money);
                lvBuy.setPayApplyRemainMoney(money);
            }
            lvBuy.setFukuan(BigDecimal.ZERO);
            lvBuy.setCreateBy(SecurityUtils.getUsername());
            lvBuyMapper.insert(lvBuy);
        }
    }



    @Override
    @Transactional
    public void addBuyReturn(LvBuy lvBuy) {
        List<GoodsSalebuyall> rows = lvBuy.getGoodsSalebuyallList();
        if (!rows.isEmpty()) {
            lvBuy.setId(IdUtils.fastSimpleUUID());
            lvBuy.setSource(BusinessConstants.BUY_SOURCE_PLEASE);
            Date ddDate = lvBuy.getDdDate();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String today = sdf.format(ddDate==null?new Date():ddDate);
            String codeMaxString = lvBuyMapper.selectMaxCode(today);
            if(StringUtils.isNotBlank(codeMaxString)){
                // 最大编号+1
                codeMaxString = Long.parseLong(codeMaxString)+1+"";
            }else{
                codeMaxString = today+"0001";
            }
            lvBuy.setNos(codeMaxString);
            lvBuy.setState("6");//22:请购中,4:请购提交，5采购中，6生成采购单
            lvBuy.setDdDate(ddDate==null?new Date():ddDate);//请购日期
            lvBuy.setCreateTime(new Date());
            lvBuy.setCustomerId(rows.get(0).getCustomerId());
            lvBuy.setCustomerName(rows.get(0).getCustomerName());
            lvBuy.setDeptId(rows.get(0).getDeptId());
            lvBuy.setTransactionType(BusinessConstants.BUY_TRANSACTION_TYPE_RETURN);

            if(lvBuy.getDingjin()!=null && lvBuy.getDingjin().compareTo(BigDecimal.ZERO)>0){
                lvBuy.setDingjin(lvBuy.getDingjin());
                lvBuy.setIsDeposit(BusinessConstants.IS_DEPOSIT_YES);
            }else{
                lvBuy.setDingjin(BigDecimal.ZERO);
                lvBuy.setIsDeposit(BusinessConstants.IS_DEPOSIT_NO);
            }
            if(lvBuy.getShippingFee()!=null && lvBuy.getShippingFee().compareTo(BigDecimal.ZERO)>0){
                lvBuy.setShippingFee(lvBuy.getShippingFee());
                lvBuy.setShippingFeePaid(BigDecimal.ZERO);
                lvBuy.setShippingFeeRemain(lvBuy.getShippingFee());
                lvBuy.setIsShippingFee(BusinessConstants.IS_DEPOSIT_YES);
                lvBuy.setDepositMerge(lvBuy.getDepositMerge());
            }else{
                lvBuy.setShippingFee(BigDecimal.ZERO);
                lvBuy.setShippingFeePaid(BigDecimal.ZERO);
                lvBuy.setShippingFeeRemain(BigDecimal.ZERO);
                lvBuy.setIsShippingFee(BusinessConstants.IS_DEPOSIT_NO);
            }

            lvBuy.setRemark(lvBuy.getRemark());
            lvBuy.setGoodsType(lvBuy.getGoodsType());
            //lvBuyMapper.insert(lvBuy);
            BigDecimal money = BigDecimal.ZERO;//订单金额
            for (GoodsSalebuyall entity : rows) {
                entity.setStatus("6");//发起请购单
                entity.setMainId(lvBuy.getId());//发起请购单
                entity.setCaigouno(lvBuy.getNos());//采购单号
                entity.setShengyuDaohuoCount(entity.getShengyu());
                entity.setShengyuDaohuoDun(entity.getDun());
                entity.setApplyRemain(entity.getGoodsBuyTotal());
                update(entity);
                money = money.add(entity.getGoodsBuyTotal());
                // 如果采购数量  小于选定数量  把差额还回请购单
                if(entity.getShengyu().compareTo(entity.getCounts())<0){
                    BigDecimal balance = entity.getCounts().subtract(entity.getShengyu());
                    List<GoodsSalebuy> goodsSalebuys = goodsSalebuyMapper.selectByCgallId(entity.getId());
                    for (GoodsSalebuy goodsSalebuy : goodsSalebuys) {
                        if(goodsSalebuy.getShengyu().compareTo(balance)>0){
                            goodsSalebuy.setShengyu(goodsSalebuy.getShengyu().subtract(balance));
                            GoodsSaledo saledo = goodsSaledoMapper.selectById(goodsSalebuy.getQinggouId());
                            if(saledo!=null){
                                saledo.setBuyRemain(saledo.getBuyRemain().add(balance));
                            }
                            balance = BigDecimal.ZERO;
                            goodsSaledoMapper.update(saledo);
                            goodsSalebuyMapper.update(goodsSalebuy);
                        }else if(goodsSalebuy.getShengyu().compareTo(balance)<=0){
                            balance = balance.subtract(goodsSalebuy.getShengyu());
                            GoodsSaledo saledo = goodsSaledoMapper.selectById(goodsSalebuy.getQinggouId());
                            if(saledo!=null){
                                saledo.setBuyRemain(saledo.getBuyRemain().add(goodsSalebuy.getShengyu()));
                            }
                            goodsSaledoMapper.update(saledo);
                            goodsSalebuyMapper.deleteById(goodsSalebuy.getId());
                        }
                    }
                }
            }
            lvBuy.setMoney(money);
            // 如果 含运费 则 计算总金额
            if(lvBuy.getShippingFee()!=null && lvBuy.getShippingFee().compareTo(BigDecimal.ZERO)>0){
                lvBuy.setShengyukp(money.add(lvBuy.getShippingFee()));
                lvBuy.setShengyu(money.add(lvBuy.getShippingFee()));
                lvBuy.setPayApplyRemainMoney(money.add(lvBuy.getShippingFee()));
            }else{
                lvBuy.setShengyukp(money);
                lvBuy.setShengyu(money);
                lvBuy.setPayApplyRemainMoney(money);
            }
            lvBuy.setFukuan(BigDecimal.ZERO);
            lvBuy.setCreateBy(SecurityUtils.getUsername());
            lvBuyMapper.insert(lvBuy);
        }
    }



    @Override
    @Transactional
    public void updateCgd(GoodsSalebuyall goodsSaledo) {
        List<GoodsSalebuyall> rows = goodsSaledo.getColumns();
        if (!rows.isEmpty()) {
            LvBuy lvBuy = new LvBuy();
            lvBuy.setId(goodsSaledo.getMainId());

            lvBuy.setState("6");//22:请购中,4:请购提交，5采购中，6生成采购单
            lvBuy.setDdDate(new Date());//请购日期
            lvBuy.setCreateTime(new Date());
            //lvBuy.setSubDate();//需求日期
            lvBuy.setCustomerId(rows.get(0).getCustomerId());
            lvBuy.setCustomerName(rows.get(0).getCustomerName());
            lvBuy.setDeptId(rows.get(0).getDeptId());
            lvBuy.setSupplierId(goodsSaledo.getSupplierId());
            lvBuy.setSupplierName(goodsSaledo.getSupplierName());
            lvBuy.setBankName(goodsSaledo.getBankName());
            lvBuy.setBankNo(goodsSaledo.getBankNo());
            lvBuy.setAccountNo(goodsSaledo.getAccountNo());
            lvBuy.setBuyer(goodsSaledo.getBuyer());
            if(goodsSaledo.getShippingFee()!=null && goodsSaledo.getShippingFee().compareTo(BigDecimal.ZERO)>0){
                lvBuy.setShippingFee(goodsSaledo.getShippingFee());
                lvBuy.setShippingFeePaid(BigDecimal.ZERO);
                lvBuy.setShippingFeeRemain(goodsSaledo.getShippingFee());
                lvBuy.setIsShippingFee(BusinessConstants.IS_DEPOSIT_YES);
                lvBuy.setDepositMerge(goodsSaledo.getDepositMerge());
            }else{
                lvBuy.setShippingFee(BigDecimal.ZERO);
                lvBuy.setShippingFeePaid(BigDecimal.ZERO);
                lvBuy.setShippingFeeRemain(BigDecimal.ZERO);
                lvBuy.setIsShippingFee(BusinessConstants.IS_DEPOSIT_NO);
            }

            if(goodsSaledo.getDingjin()!=null && goodsSaledo.getDingjin().compareTo(BigDecimal.ZERO)>0){
                lvBuy.setDingjin(goodsSaledo.getDingjin());
                lvBuy.setIsDeposit(BusinessConstants.IS_DEPOSIT_YES);
            }else{
                lvBuy.setDingjin(BigDecimal.ZERO);
                lvBuy.setIsDeposit(BusinessConstants.IS_DEPOSIT_NO);
            }
            lvBuy.setDingjin(goodsSaledo.getDingjin());
            lvBuy.setRemark(goodsSaledo.getRemark());
            lvBuy.setGoodsType(goodsSaledo.getGoodsType());
            //lvBuyMapper.insert(lvBuy);
            BigDecimal money = BigDecimal.ZERO;//订单金额
            for (GoodsSalebuyall entity : rows) {
                entity.setStatus("6");//发起请购单
                entity.setMainId(lvBuy.getId());//发起请购单
                entity.setCaigouno(lvBuy.getNos());//采购单号
                entity.setShengyuDaohuoCount(entity.getShengyu());
                entity.setSupplierId(lvBuy.getSupplierId());
                entity.setSupplierName(lvBuy.getSupplierName());
                entity.setShengyuDaohuoDun(entity.getDun());
                entity.setApplyRemain(entity.getGoodsBuyTotal());
                entity.setApplyMoney(BigDecimal.ZERO);
                entity.setApplyRemain(entity.getGoodsBuyTotal());
                update(entity);
                money = money.add(entity.getGoodsBuyTotal());
            }

            lvBuy.setMoney(money);
            if(goodsSaledo.getShippingFee()!=null && goodsSaledo.getShippingFee().compareTo(BigDecimal.ZERO)>0){
                lvBuy.setShengyukp(money.add(goodsSaledo.getShippingFee()));
                lvBuy.setShengyu(money.add(goodsSaledo.getShippingFee()));
                lvBuy.setPayApplyRemainMoney(money.add(goodsSaledo.getShippingFee()));
            }else{
                lvBuy.setShengyukp(money);
                lvBuy.setShengyu(money);
                lvBuy.setPayApplyRemainMoney(money);
            }
            lvBuy.setDingjin(lvBuy.getDingjin());
            lvBuy.setFukuan(BigDecimal.ZERO);
            lvBuyMapper.update(lvBuy);
        }
    }


    public void updateQgCg(String caigouNo, String mainId, String id, String status) {
        List<GoodsSalebuy> goodsSalebuyList = goodsSalebuyMapper.selectByCgallId(id);
        for (GoodsSalebuy goodsSalebuy : goodsSalebuyList) {
            goodsSalebuy.setMainId(mainId);
            goodsSalebuy.setCaigouno(caigouNo);
            goodsSalebuy.setStatus(status);
            goodsSalebuyMapper.update(goodsSalebuy);
            GoodsSaledo goodsSaledo = goodsSaledoMapper.selectById(goodsSalebuy.getQinggouId());
            goodsSaledo.setStatus(BusinessConstants.SALE_TYPE_12);
            goodsSaledo.setCaigouId(mainId);
            goodsSaledo.setCaigouno(caigouNo);
            goodsSaledo.setGoodssalebuyallId(id);
            goodsSaledo.setGoodssalebuyId(goodsSalebuy.getId());
            goodsSaledoMapper.update(goodsSaledo);
            if(goodsSaledo!=null && goodsSaledo.getGoodsSaleId()!=null ){
                 iGoodsSaleService.updateStatusById(goodsSaledo.getGoodsSaleId(), BusinessConstants.SALE_TYPE_12);
            }
        }
    }

    public void updateDaohuo(String daohuoId, String nos, String id, String status) {
        List<GoodsSalebuy> goodsSalebuyList = goodsSalebuyMapper.selectByCgallId(id);
        for (GoodsSalebuy goodsSalebuy : goodsSalebuyList) {
            goodsSalebuy.setDaohuoId(daohuoId);
            goodsSalebuy.setDaohuono(nos);
            goodsSalebuy.setStatus(status);
            goodsSalebuyMapper.update(goodsSalebuy);
            GoodsSaledo goodsSaledo = goodsSaledoMapper.selectById(goodsSalebuy.getQinggouId());
            goodsSaledo.setStatus(BusinessConstants.SALE_TYPE_10);
            goodsSaledo.setDaohuoId(daohuoId);
            goodsSaledo.setDaohuono(nos);
            goodsSaledoMapper.update(goodsSaledo);
        }

    }

    @Override
    public int getUnarrivedGoodsCount(GoodsSalebuyall entity) {
        return goodsSalebuyallMapper.getUnarrivedGoodsCount(entity);
    }

    @Override
    public void submitBuySelf(LvBuy entity) {
        BigDecimal money  = BigDecimal.ZERO;
        List<GoodsSalebuyall> goodsSalebuyallList = entity.getGoodsSalebuyallList();
        for (GoodsSalebuyall goodsSalebuyall : goodsSalebuyallList) {
            money = money.add(goodsSalebuyall.getGoodsBuyTotal());
        }
        entity.setSource(BusinessConstants.BUY_SOURCE_SELF);
        entity.setMoney(money);

        if(entity.getDingjin()!=null && entity.getDingjin().compareTo(BigDecimal.ZERO)>0){
            entity.setDingjin(entity.getDingjin());
            entity.setIsDeposit(BusinessConstants.IS_DEPOSIT_YES);
        }else{
            entity.setDingjin(BigDecimal.ZERO);
            entity.setIsDeposit(BusinessConstants.IS_DEPOSIT_NO);
        }
        if(entity.getShippingFee()!=null && entity.getShippingFee().compareTo(BigDecimal.ZERO)>0){
            entity.setShippingFee(entity.getShippingFee());
            entity.setShippingFeePaid(BigDecimal.ZERO);
            entity.setShippingFeeRemain(entity.getShippingFee());
            entity.setIsShippingFee(BusinessConstants.IS_DEPOSIT_YES);
            entity.setDepositMerge(entity.getDepositMerge());
        }else{
            entity.setShippingFee(BigDecimal.ZERO);
            entity.setShippingFeePaid(BigDecimal.ZERO);
            entity.setShippingFeeRemain(BigDecimal.ZERO);
            entity.setIsShippingFee(BusinessConstants.IS_DEPOSIT_NO);
        }


        lvBuyService.insert(entity);
        for (GoodsSalebuyall goodsSalebuyall : goodsSalebuyallList) {
            goodsSalebuyall.setMainId(entity.getId());
            goodsSalebuyall.setCaigouno(entity.getNos());
            goodsSalebuyall.setShengyuDaohuoCount(goodsSalebuyall.getShengyu());
            goodsSalebuyall.setShengyuDaohuoDun(goodsSalebuyall.getDun());
            goodsSalebuyall.setApplyRemain(goodsSalebuyall.getGoodsBuyTotal());
            insert(goodsSalebuyall);
        }
    }

    @Override
    public void updateBuySelf(LvBuy entity) {
        BigDecimal money  = BigDecimal.ZERO;
        List<GoodsSalebuyall> goodsSalebuyallList = entity.getGoodsSalebuyallList();
        for (GoodsSalebuyall goodsSalebuyall : goodsSalebuyallList) {
            money = money.add(goodsSalebuyall.getGoodsBuyTotal());
        }
        entity.setSource(BusinessConstants.BUY_SOURCE_SELF);
        entity.setMoney(money);
        lvBuyService.update(entity);
        // 先删除 之前 的明细表
        goodsSalebuyallMapper.deleteByBuyId(entity.getId());
        for (GoodsSalebuyall goodsSalebuyall : goodsSalebuyallList) {
            goodsSalebuyall.setMainId(entity.getId());
            goodsSalebuyall.setCaigouno(entity.getNos());
            goodsSalebuyall.setShengyuDaohuoCount(goodsSalebuyall.getShengyu());
            goodsSalebuyall.setShengyuDaohuoDun(goodsSalebuyall.getDun());
            goodsSalebuyall.setApplyRemain(goodsSalebuyall.getGoodsBuyTotal());
            insert(goodsSalebuyall);
        }
    }

    @Override
    public GoodsSalebuyall selectByGoodsIdAndRemark(String goodsId, String remark, String username) {
        return goodsSalebuyallMapper.selectByGoodsIdAndRemark(goodsId,remark,username);
    }


}
